using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace Chess
{
    public class CoVua_Control : ControlSuper
    {
        private static QuanCoVua[] DSQuanCoVuaKhoiTao = { QuanCoVua.XeDen, QuanCoVua.MaDen, QuanCoVua.TuongDen, QuanCoVua.HauDen, QuanCoVua.VuaDen, QuanCoVua.TuongDen, QuanCoVua.MaDen, QuanCoVua.XeDen };

        public override void KhoiTao(int qcDiTruoc)
        {
            bc = new BanCoVua();
            _laCo = 0;
            _ktBanCo.X = 240;
            _ktBanCo.Y = 240;
            _ktOCo = 30;
            _soOCo = 8;
            _csHinhNext = 13;
            bc.KhoiTaoBanCo(qcDiTruoc);
            dsQC.Clear();
            bc.DanhSachQuanCo(ref dsQC);
            qcDuocPhepDi = qcDiTruoc;
            _thTren = false;
        }

        public override void LoadTrangThai(byte[] dl)
        {
            bc = new BanCoVua();
            _laCo = 0;
            _ktBanCo.X = 240;
            _ktBanCo.Y = 240;
            _ktOCo = 30;
            _soOCo = 8;
            _csHinhNext = 13;

            bc.LoadBanCo(dl);
            dsQC.Clear();
            bc.DanhSachQuanCo(ref dsQC);
            qcDuocPhepDi = (int)dl[0];
            _thTren = false;
        }

        public override Hashtable[] DiMotNuocCo(int vtDau, int vtCuoi, int loaiPhong)
        {
            Hashtable[] kq = new Hashtable[3];
            kq[0] = new Hashtable();
            kq[1] = new Hashtable();
            kq[2] = new Hashtable();
            QuanCoVua temp1 = QuanCoVua.OTrong;
            QuanCoVua temp2 = QuanCoVua.OTrong;

            if (!dsQC.ContainsKey(vtDau))
                return null;

            temp1 = (QuanCoVua)dsQC[vtDau];
            dsQC.Remove(vtDau);
            if (dsQC.ContainsKey(vtCuoi))
            {
                temp2 = (QuanCoVua)dsQC[vtCuoi];
                dsQC.Remove(vtCuoi);
            }

            foreach (int each in dsQC.Keys)
            {
                kq[0].Add(each, dsQC[each]);
            }
            kq[1].Add(vtDau, temp1);
            kq[1].Add(vtCuoi, temp2);
            int[] kqDi = bc.DiMotNuocCo(vtDau, vtCuoi, loaiPhong);
            if (kqDi != null)
            {
                if (kqDi[0] != -1)
                {
                    kq[2].Add(vtCuoi, kqDi[1]);
                    dsQC.Add(vtCuoi, kqDi[1]);
                }
                else
                {
                    kq[2].Add(vtCuoi, temp1);
                    dsQC.Add(vtCuoi, temp1);
                }
                qcDuocPhepDi = (qcDuocPhepDi == 0) ? 1 : 0;
            }
            else
            {
                dsQC.Add(vtDau, temp1);
                if(temp2 != QuanCoVua.OTrong)
                    dsQC.Add(vtCuoi, temp2);
                return null;
            }

            return kq;
        }

        public override Hashtable[] DiMotNuocCo(int vtDau, int vtCuoi)
        {
            return DiMotNuocCo(vtDau, vtCuoi, 1);
        }
    }
}
